/**
 * 数据类型映射页面JS
 * User: zhaoming
 * Date: 201616-07-05
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){

    //激活左侧导航
    activeNavBar("dataTypeMapping", "basic");

    //加载数据列表
    loadGrid();

    //重置数据列表宽度
    $("#gridTableWidgetBody").resize(function(){
        $("#gridTable").setGridWidth($(this).width(), false);
    });

});

/**
 * 加载数据列表
 */
function loadGrid(){
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype:"POST",
        url: "/web/dataTypeMapping/selectAll.json",
        datatype: "json",
        autowidth: true,
        height: "450",
        colModel:[
            {name:'databaseType',label:'Database数据类型', index:'databaseType', width:100, fixed:false, sortable: false, align:'center'},
            {name:'jdbcType',label:'Jdbc数据类型', index:'jdbcType', width:100, fixed:false, sortable: false, align:'center', editable : true},
            {name:'javaType',label:'Java数据类型', index:'javaType', width:100, fixed:false, sortable: false, align:'center', editable : true},
            {name:'', label:'操作', width:250, fixed:true, sortable:false, align:'center', frozen:true,
                formatter:function(cellValue,options,rowObject){
                    var context = "";
                    context += '<button id="edit'+options.rowId+'" class="btn btn-white btn-info btn-bold" onclick="edit('+options.rowId+')"><i class="ace-icon fa fa-pencil bigger-120 blue"></i>修改</button> ';
                    context += '<button id="save'+options.rowId+'" class="btn btn-white btn-info btn-bold" onclick="save('+options.rowId+')" disabled><i class="ace-icon fa fa-pencil bigger-120 blue"></i>保存</button> ';
                    context += '<button id="cancel'+options.rowId+'" class="btn btn-white btn-info btn-bold" onclick="cancel('+options.rowId+')" disabled><i class="ace-icon fa fa-pencil bigger-120 blue"></i>取消</button> ';
                    return context;
                }
            }
        ],
        rownumbers:true,//添加左侧行号
        viewrecords : true,
        rowNum:1000,
        altRows: true,
        //altclass: "",
        pager : "#gridPager",
        jsonReader: {
            root: "root", //对于json中数据列表
        },
        loadComplete : function() {
            updatePagerIcons(this);
            $(this).setGridWidth($("#gridTableWidgetBody").width(), true);
        }
    });
}

/**
 * 编辑
 * @param selectedId 行号
 */
function edit(selectedId){
    jQuery("#gridTable").jqGrid('editRow', selectedId, {
        keys : false,
        oneditfunc: function() {
            jQuery("#edit"+selectedId).attr("disabled", true);
            jQuery("#save"+selectedId).attr("disabled", false);
            jQuery("#cancel"+selectedId).attr("disabled", false);
        }
    });
}

/**
 * 保存
 * @param selectedId 行号
 */
function save(selectedId){
    var rowData = $("#gridTable").jqGrid("getRowData", selectedId);
    jQuery("#gridTable").jqGrid('saveRow', selectedId, {
        url: '/web/dataTypeMapping/update',
        extraparam : {
            databaseType : rowData.databaseType
        },
        successfunc: function(response) {
            //成功
            jQuery("#edit"+selectedId).attr("disabled", false);
            jQuery("#save"+selectedId).attr("disabled", true);
            jQuery("#cancel"+selectedId).attr("disabled", true);
            return true;
        },
        afterrestorefunc: function(){
            //失败
        }
    });
}

/**
 * 取消
 * @param selectedId 行号
 */
function cancel(selectedId){
    jQuery("#gridTable").jqGrid('restoreRow', selectedId, {
        "afterrestorefunc" : function(response){
            jQuery("#edit"+selectedId).attr("disabled", false);
            jQuery("#save"+selectedId).attr("disabled", true);
            jQuery("#cancel"+selectedId).attr("disabled", true);
        }
    });
}